package com.gtpickupapp;

import java.util.ArrayList;

import android.app.ActivityGroup;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;



public class MyEventsGroup extends ActivityGroup {

		public AlertDialog.Builder exitAlert;
	
		public static MyEventsGroup group;
		private ArrayList<View> history;
		
		
		@Override  
	    protected void onCreate(Bundle savedInstanceState) {  
	          super.onCreate(savedInstanceState);  
	          this.history = new ArrayList<View>();  
	          setupAlerts();
	          group = this;  
	          Bundle bundle = getIntent().getExtras();
	          Intent intent = new  Intent(this,MyEvents.class);
	          intent.putExtras(bundle);
	              // Start the root activity within the group and get its view  
	          View view = getLocalActivityManager().startActivity("my",  intent 
	                                            .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))  
	                                            .getDecorView();  
	  
	              // Replace the view of this ActivityGroup  
	          replaceView(view);  
	  
	       }  
		
		 public void replaceView(View v) {  
             // Adds the old one to history  
			 history.add(v);  
             // Changes this Groups View to the new View.  
			 setContentView(v);  
		 }
		 
		 public void back() {  
			 //TODO WTF??? 000000
	         if(history.size() > 1) {  
	        	 //Log.e("3333333333333", "WTF!!!");
	             history.remove(history.size()-1);  
	             setContentView(history.get(history.size()-1));  
	           
	         }else { 
	        	 exitAlert.create().show();
	             //finish();  
	         }  
		  } 
		 
		 @Override  
		 public void onBackPressed() {  
			 MyEventsGroup.group.back();  
		     return;  
		 }
		 
		 public void setupAlerts(){
			 exitAlert = new AlertDialog.Builder(this);
		        exitAlert.setMessage("Are You Sure You Want To Exit The Application")
		        	.setCancelable(false).setPositiveButton("OK", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							finish();
						}
					})
					.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							dialog.cancel();
						}
					});
		        
		 }
}
